/*
Copyright (c) 2009 Vratislav Cermak (vratacermak@gmail.com)
 
This file is part of TeachMe Project. See http://code.google.com/p/teach-me for more information.
 
TeachMe is an open source project. Using of TeachMe is free of charge.
It is distributed under GNU General Public License version 3 (GPLv3).
GPLv3 can be found at http://www.gnu.org/licenses/gpl.html.
 
This program is distributed in the hope that it will be useful, but WITHOUT
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
FOR A PARTICULAR PURPOSE.  See the license for more details.
*/

using System.Text;
using System.Text.RegularExpressions;

namespace TeachMe.Application
{
    public class Sentence
    {
        private readonly StringBuilder _text = new StringBuilder();

        public void Add(string value)
        {
            _text.Append(value + " ");
        }

        public string Text
        {
            get
            {
                return Compile(_text.ToString());
            }
        }

        private static string Compile(string value)
        {
            value = Regex.Replace(value, "[ ]+", " ");
            value = Regex.Replace(value, "^[\\W]+", "");
            value = Regex.Replace(value, "[- ]+$", "");
            value = Regex.Replace(value, "[-]{2,}[\\W]*", "-- ");
            value = Regex.Replace(value, "[ ]*,[\\W]+", ", ");
            
            if (value.Length > 0)
            {
                value = value.Substring(0, 1).ToUpper() + value.Substring(1);
            }
                
            return value;
        }

        public override string ToString()
        {
            return Text;
        }

        public override int GetHashCode()
        {
            return Text.GetHashCode();
        }
    }
}